package com.artezio.task.model;

import com.artezio.task.dto.FormatDataDto;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

import static javax.persistence.GenerationType.AUTO;
import static javax.persistence.GenerationType.IDENTITY;

@Entity
@Table(name = "contact_tel_detail")
public class ContactTelDetail implements Serializable, Comparable<ContactTelDetail> {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private int version;
    private String telType;
    private String telNumber;
    private Contact contact;

    public ContactTelDetail() {
    }

    public ContactTelDetail(String telType, String telNumber) {
        this.telType = telType;
        this.telNumber = telNumber;
    }

    public String toString() {
        return "Contact Tel Detail - Id: " + id + ", Contact id: "
                + getContact().getId() + ", Type: " + telType + ", Number: "
                + telNumber;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "TEL_TYPE")
    public String getTelType() {
        return telType;
    }

    public void setTelType(String telType) {
        this.telType = telType;
    }

    @Column(name = "TEL_NUMBER")
    public String getTelNumber() {
        return telNumber;
    }

    public void setTelNumber(String telNumber) {
        this.telNumber = telNumber;
    }

    @Version
    @Column(name = "VERSION")
    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTACT_ID", nullable = false)
    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public void initByDto(FormatDataDto dto) {
        if (dto.getId() != 0) {
            int id = dto.getId();
        }
        //int version;
        telType = dto.getTypeNumTwo();
        telNumber = dto.getNumberTwo();
        //Contact contact;

    }

    public void newInit(FormatDataDto dto) {
        telType = dto.getTypeNumTwo();
        telNumber = dto.getNumberTwo();
    }

    public int compareTo(ContactTelDetail other) {
        if (!this.telType.equalsIgnoreCase(other.getTelType()))
            return this.telType.compareTo(other.getTelType());
        return this.getId()-other.getId();
    }


}
